#!/usr/bin/env python3
"""
LLM Enhanced Voice Control System Launch File

启动LLM增强语音控制系统，让EchoKit Server的LLM处理复杂语音命令
支持与视觉交互系统的无缝集成

Architecture: [EchoKit Server LLM] → [Structured Commands] → [LLM Enhanced Voice Processor] → [ROS2 Topics] → [micro-ros-agent] → [EchokitBot UDP]

This launch file starts:
1. LLM Bridge Voice Control Node (connects to EchoKit Server and receives structured commands)
2. LLM Enhanced Voice Processor (executes structured commands with precise parameters)
3. System Monitor (monitors system health)
4. Voice-Vision Integration Bridge (optional, for vision-enabled systems)
"""

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

def generate_launch_description():
    # Declare launch arguments
    server_url_arg = DeclareLaunchArgument(
        'server_url',
        default_value='ws://<ECHO_SERVER>:8060/ws/robot',
        description='EchoKit server WebSocket URL'
    )
    
    log_level_arg = DeclareLaunchArgument(
        'log_level',
        default_value='info',
        description='Log level (debug, info, warn, error)'
    )
    
    enable_vision_integration_arg = DeclareLaunchArgument(
        'enable_vision_integration',
        default_value='false',
        description='Enable voice-vision integration bridge'
    )
    
    # LLM Bridge Voice Control Node (connects to EchoKit Server)
    llm_bridge_node = Node(
        package='echokitbot_microros_voice',
        executable='llm_bridge_voice_control_node',
        name='llm_bridge_voice_control_node',
        parameters=[{
            'server_url': LaunchConfiguration('server_url'),
            'reconnect_interval': 10.0,
            'connection_timeout': 30.0,
            'enable_vision_integration': LaunchConfiguration('enable_vision_integration'),
        }],
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')]
    )
    
    # LLM Enhanced Voice Processor (executes structured commands)
    llm_enhanced_processor = Node(
        package='echokitbot_microros_voice',
        executable='llm_enhanced_voice_processor',
        name='llm_enhanced_voice_processor',
        parameters=[{
            'motion_config.linear_speed': 0.3,
            'motion_config.angular_speed': 1.0,
            'motion_config.angle_tolerance': 5.0,
            'motion_config.distance_tolerance': 0.05
        }],
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')]
    )
    
    # System Monitor (monitors system health)
        system_monitor = Node(
        package='echokitbot_microros_voice',
        executable='system_monitor',
        name='system_monitor',
        parameters=[{
            'echokitbot_ip': '<ECHOKIT_IP>',
            'echokit_server': '<ECHO_SERVER>:8060',
            'monitor_vision_system': LaunchConfiguration('enable_vision_integration'),
        }],
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')]
    )
    
    # Voice-Vision Integration Bridge (optional)
    voice_vision_bridge = Node(
        package='echokitbot_microros_voice',
        executable='voice_vision_integration',
        name='voice_vision_integration',
        parameters=[{
            'voice_trigger_keywords': ['你看到了什么', '描述一下', '前面有什么', '这是什么'],
            'vision_response_timeout': 10.0,
        }],
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')],
        condition=IfCondition(LaunchConfiguration('enable_vision_integration'))
    )
    
    return LaunchDescription([
        server_url_arg,
        log_level_arg,
        enable_vision_integration_arg,
        
        LogInfo(msg='Starting LLM Enhanced Voice Control System...'),
        LogInfo(msg=['EchoKit Server: ', LaunchConfiguration('server_url')]),
        LogInfo(msg='Architecture: EchoKit LLM → Structured Commands → LLM Enhanced Processor → ROS2 Topics → micro-ros-agent → EchokitBot'),
        LogInfo(msg='LLM Features: Intelligent command parsing, Structured JSON output, Precise parameter calculation'),
        LogInfo(msg='Supported Commands: "左转90度", "前进2米", "右转180度", "后退1米", etc.'),
        LogInfo(msg='Vision Integration: Voice-triggered visual analysis and feedback'),
        LogInfo(msg='Expected micro-ros-agent on localhost:8888 (UDP)'),
        LogInfo(msg='Expected EchokitBot at <ECHOKIT_IP> (via micro-ROS)'),
        
        llm_bridge_node,
        llm_enhanced_processor,
        system_monitor,
        voice_vision_bridge,
    ])